﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IsmsExplorer.IsmsDataImport.UI.Common;
using IsmsExplorer.Application;
using System.Diagnostics;

namespace IsmsExplorer.IsmsDataImport.UI {
    public partial class Form1 : Form {
        // *************************************** Data ***************************************


        // *************************************** Construction ***************************************
        public Form1() {
            InitializeComponent();
        }


        // *************************************** Operations ***************************************
        private void mnuMainFileImportAssetRiskRegister_Click(object sender, EventArgs e) {
            NewProjectForm f = new NewProjectForm();
            if (f.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
                // Assign project we're working on to global session.
                UserSession.CurrentProject = f.Project;

                // Get the name of file with ASSET Risk Register data.
                string fileName = UIHelper.BrowseForFileOpen(title: "Select Asset Risk Register data file", owner: this);
                if (!string.IsNullOrEmpty(fileName)) {
                    // Parse file information.
                    Parser.AssetRiskRegisterDataParser parser = new Parser.AssetRiskRegisterDataParser();
                    parser.FileName = fileName;
                    parser.Project = UserSession.CurrentProject;
                    try {
                        Cursor = Cursors.WaitCursor;
                        // Do the parsing.
                        parser.Parse();
                        UIHelper.ShowInfoMessage(this, "Parsing of ISMS data has been successful!");

                    } catch (Exception ex) {
                        UIHelper.ShowErrorMessage(this, ex.Message);

                    } finally {
                        Cursor = Cursors.Default;
                    }
                }
            }
        }

        private void mnuMainFileExit_Click(object sender, EventArgs e) {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e) {

        }

        /// <summary>
        /// Asks user to specify the root folder under which all documents reside. Then recursively visits
        /// all folders and subfolders, finds all MS Word (.doc, .docx) documents, gets each documents' bookmark
        /// and associates the ISO 27001 control by reference ID. The bookmark is expected to contain a 
        /// comma-separated list of reference IDs.
        /// A log file will be created at the root, named "ISODocumentMap-[datetime].txt" where [datetime] is 
        /// the current date and time (to avoid name clashes), detailing which files were found, bookmarks,
        /// which were valid etc.
        /// Files which failed to map will have to be either fixed or manually mapped (via the API/UI).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuMainFileLinkISMSDocumentsToISOControls_Click(object sender, EventArgs e) {
            if (UserSession.CurrentProject == null) {
                SelectCurrentProject();
                return;
            }
            string rootFolder = UIHelper.BrowseForFolder(title: "Select root folder where ISMS documents reside", owner: this);
            if (rootFolder != null) {
                Parser.MsWordIsoBookmarkParser parser = new Parser.MsWordIsoBookmarkParser();
                try {
                    Cursor = Cursors.WaitCursor;
                    // Configure parser with source (root) folder, ask for recursive visit into all subfolders.
                    parser.InputFolder = rootFolder;
                    parser.Recursive = true;
                    // Do the parsing.
                    parser.Parse();

                    // User the output to link controls to documents.
                    Parser.IsmsDocumentToControlMapper mapper = new Parser.IsmsDocumentToControlMapper();
                    // Mapper uses same log file, appending to it!
                    mapper.LogFileName = parser.LogFileName;
                    mapper.MapControlToDocuments(parser.ControlToDocumentsMap);

                    if (UIHelper.ShowYesNoQuestion(this, "Linking of ISMS documents to controls has been completed! Want to check log file? (Y/N)", MessageBoxDefaultButton.Button1) 
                        == System.Windows.Forms.DialogResult.Yes) 
                    {
                        Process.Start(parser.LogFileName);
                    }

                } catch (Exception ex) {
                    UIHelper.ShowErrorMessage(this, ex.Message);

                } finally {
                    Cursor = Cursors.Default;
                }
            }
        }

        private void SelectCurrentProject() {
            SelectProjectForm f = new SelectProjectForm();
            f.ShowDialog();
        }

    } // end public partial class Form1 : Form
}
